package Logic;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Database.AuditLogDB;
import Model.Approval;
import Model.StaffAppointment;

import com.mysql.jdbc.DatabaseMetaData;

/**
 * Servlet implementation class StaffAppNew_BLL
 */
@WebServlet("/StaffApptNew_BLL")
public class StaffAppNew_BLL extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public StaffAppNew_BLL() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

		// javax.servlet.http.HttpSession session = request.getSession();
		// String username = session.getAttribute("username").toString();

		if (request.getParameter("effectivedate") != ""
				&& request.getParameter("staff") != "") {
			int groupNumber = -1;

			String staffs = request.getParameter("staff");
			String[] stafflist = staffs.split(", ");

			String appointmentid = request.getParameter("appointmentnormal");
			System.out.println("Appointment: " + appointmentid);

			String description = request.getParameter("description");

			boolean exceedLimit = false;

			String effectiveDate = request.getParameter("effectivedate");

			if (description != null && description != "") {
				// New Group to be created
				System.out.println("Creating new sub group");
				groupNumber = Database.StaffAppointmentGroupDB.getLastNumber() + 1;
				Database.StaffAppointmentGroupDB.createGroup(
						Integer.toString(groupNumber), description);
			} else {
				groupNumber = 1;
			}
			
			String tempStaff = "";
			String tempAppointment = appointmentid;
			// loop for creating same appointment for multiple staff
			for (String staffid : stafflist) {
				//for (String appointmentid1 : appointmentlist) {

					String md5 = Logic.MD5.convert(Long.toString(System
							.currentTimeMillis()) + staffid);

					if (Database.StaffAppointmentDB
							.numberOfAppointments(staffid) >= Database.MaxConcApptDB
							.select()) {
						exceedLimit = true;
					}

					System.out.println("Appointments:"
							+ Database.StaffAppointmentDB
									.numberOfAppointments(staffid));
					System.out.println("Max Conc:"
							+ Database.MaxConcApptDB.select());

					StaffAppointment sa = new StaffAppointment();
					sa.setAppointmentID(appointmentid);
					sa.setEffectiveDate(effectiveDate);
					sa.setApptGrpID(Integer.toString(groupNumber));
					sa.setLoginID(staffid);
					sa.setMd5(md5);
					sa.setCreatedBy(
							(String) request.getSession().getAttribute("username"));
					if (Database.StaffAppointmentDB
							.checkDuplicateAppointmentEntry(sa)) {
						tempStaff = staffid;
						response.sendRedirect("staffapp_newappt.jsp?error=3&staffid=" + tempStaff);
						return;
					}
					Database.StaffAppointmentDB.createAppointment(sa);
					if (exceedLimit) {
						response.sendRedirect("staffapp_newappt.jsp?prompt=true&id="
								+ md5);
						return;
					}
				//}
			}

			AuditLogDB.insertLog("assign staff to appointment",
					(String) request.getSession().getAttribute("name"));
			response.sendRedirect("staffapp_newappt.jsp?error=-1");
		} else {
			response.sendRedirect("staffapp_newappt.jsp?error=1");
		}

	}

}
